﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MSOutlook = Microsoft.Office.Interop.Outlook;
using Utility = Microsoft.Sample.Outlook.Utility;

namespace Microsoft.Sample.Outlook.CRMIntegration.ItemForms
{
    public partial class AppointmentForm : Form
    {
        Utility.ItemAdapter _appointment;

        public AppointmentForm()
        {
            InitializeComponent();
        }

        public AppointmentForm(Utility.ItemAdapter item)
        {
            InitializeComponent();
            if (item.Class != MSOutlook.OlObjectClass.olAppointment)
                throw new ApplicationException("ActivityForm can only be initalized with outlook task items");

            _appointment = item;
        }

        private Guid? PersonalFolderID //get account id
        {
            get 
            {
                if (_appointment == null)
                {
                    return null;
                }

                MSOutlook.MAPIFolder calendarFolder = (MSOutlook.MAPIFolder)_appointment.Parent;
                MSOutlook.MAPIFolder personalFolder = (MSOutlook.MAPIFolder)calendarFolder.Parent;

                PIMDataSetTableAdapters.AccountsTableAdapter accountsTableAdapter = new PIMDataSetTableAdapters.AccountsTableAdapter();
                return (Guid)accountsTableAdapter.GetAccountIDByOutlookID(personalFolder.EntryID);
            }
        }

        private PIMDataSet.AppointmentsRow CurrentRow //get current row appointment
        {
            get
            {
                DataRowView view = (DataRowView)appointmentsBindingSource.Current;
                return (PIMDataSet.AppointmentsRow)view.Row;
            }
        }

        private void AppointmentForm_Load(object sender, EventArgs e)
        {
            this.accountsTableAdapter.Fill(this.pimDataSet.Accounts);
            if (_appointment == null)//new appointment item from personal appointment folder
            {
                appointmentsBindingSource.AddNew();
                CurrentRow.LocalID = Guid.NewGuid();

                //comboBoxA.Enabled = true;
            }
            else if (_appointment.EntryID == null) //define an appointment that copied from another appointment folder
            {
                appointmentsBindingSource.AddNew();
                CurrentRow.LocalID = Guid.NewGuid();
                CurrentRow.AccountID = PersonalFolderID.Value;
                //comboBoxAccount.SelectedValue = AccountFolderID.Value; //get account id to bind Selected value
            }
            else //Open existing value
            {
                this.appointmentsTableAdapter.FillByOutlookID(this.pimDataSet.Appointments, _appointment.EntryID);
            }
        }

        private void btnCalcel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (ValidateChildren() == false)
            {
                return;
            }

            appointmentsBindingSource.EndEdit();

            PIMDataSet.AppointmentsRow row = this.CurrentRow;

            if (_appointment == null)
            {
                string entryID = (string)accountsTableAdapter.GetOutlookID(Utility.GlobalClass.AccountID);
                MSOutlook.MAPIFolder personalfolder = Globals.ThisApplication.Application.Session.GetFolderFromID(
                    entryID, Globals.ThisApplication.Application.ActiveExplorer().CurrentFolder.StoreID);

                MSOutlook.MAPIFolder folder = Utility.Folder.GetFolder(personalfolder, Properties.Resources.AppointmentsFolderName);
                _appointment = Utility.ItemAdapter.FromObject(folder.Items.Add(MSOutlook.OlItemType.olAppointmentItem));
            }

            if (Utility.SyncEngine.SyncItem<PIMDataSet.AppointmentsRow>(row, _appointment, Utility.SyncDirection.ItemToOutlook))
                _appointment.Save();

            if (CurrentRow.OutlookID != _appointment.EntryID)
                CurrentRow.OutlookID = _appointment.EntryID;

            int i = this.appointmentsTableAdapter.Update(this.pimDataSet.Appointments);
            
            this.Close();
        }
    }
}
